/* 
 * File:   main.cpp
 * Author: Connor Willison
 *
 * Created on March 4, 2012, 12:33 PM
 * Contains the WinMain api entry point
 * and window procedure.
 */

#include <windows.h>
#include <iostream>

//direct 2d
#include <d2d1.h>

using namespace std;

//prototypes

LRESULT CALLBACK WindowProcedure
(HWND hwnd, unsigned int message, WPARAM wParam, LPARAM lParam);

//WinMain - entry point for api

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,
        char * cmdParam, int cmdShow) {

    char className [] = "Basic Window";

    //first, create the window class
    WNDCLASS _class;
    _class.style = 0;
    _class.lpfnWndProc = WindowProcedure; // window procedure: mandatory
    _class.cbClsExtra = 0;
    _class.cbWndExtra = 0;
    _class.hInstance = hInst; // owner of the class: mandatory
    _class.hIcon = 0;
    _class.hCursor = ::LoadCursor(0, IDC_ARROW); // optional
    _class.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); // optional
    _class.lpszMenuName = 0;
    _class.lpszClassName = className; // mandatory

    //register the class
    ::RegisterClass(& _class);

    //now create the window
    HWND _hwnd;

    _hwnd = ::CreateWindow(
            className, // name of a registered window class
            "Gamepad Test", // window caption
            WS_OVERLAPPEDWINDOW, // window style
            CW_USEDEFAULT, // x position
            CW_USEDEFAULT, // y position
            CW_USEDEFAULT, // witdh
            CW_USEDEFAULT, // height
            0, // handle to parent window
            0, // handle to menu
            hInst, // application instance
            0); // window creation data

    //show the window
    ::ShowWindow(_hwnd, cmdShow);

    //update the window
    ::UpdateWindow(_hwnd);
    
    MSG msg;

    while (true) {
        //get messages from queue
        PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);

        // If the message is WM_QUIT, exit the while loop
        if (&msg != NULL) {
            //stop program if needed
            if (msg.message == WM_QUIT) {
                break;
            }

            // Translate the message and dispatch it to WindowProc()
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        ::UpdateWindow(_hwnd);

        ::Sleep(20);

    }

    return msg.wParam;
}

// Window Procedure called by Windows

LRESULT CALLBACK WindowProcedure
(HWND hwnd, unsigned int message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
        case WM_DESTROY:
        {
            ::PostQuitMessage(0);
            return 0;
        }
        default:
        {
            return ::DefWindowProc(hwnd, message, wParam, lParam);
        }

    }
}
